function Table2

addpath('basic_functions')
addpath('../Data')

Nboot = 1000;

[Return,Z,IsF,Nt] = package_data_all_greeks;
Return = Return.ret_daily;

[N,T,L] = size(Z);

% construct W and X matrices that will be used for IPCA
W = NaN(L,L,T);
X = NaN(L,T);
for t = 1:T-1
    W(:,:,t) = squeeze(Z(IsF(:,t+1),t,:))'*squeeze(Z(IsF(:,t+1),t,:)) / Nt(t+1);
    X(:,t+1) = squeeze(Z(IsF(:,t+1),t,:))'*Return(IsF(:,t+1),t+1) / Nt(t+1);
    Return(~IsF(:,t+1),t+1) = NaN;
end
clear t;

for K = 1:6
    % zero alpha
    [G, F] = IPCA(X,W,Nt,K);
    r2 = IPCA_R2(G,F,Return,Z,IsF,X,W);

    tt = sqrt(T-2)*mean(F,2,'omitnan')./std(F,[],2,'omitnan');
    R2.NPfactors(1,K) = sum(tt<1.96);

    Exp2 = NaN(K,T);
    for j = 1:K
        for t = 1:T-1
            Exp2(j,t) = mean((squeeze(Z(:,t,:),'omitnan')*G(:,j))*F(j,t+1));
        end
    end
    tt = sqrt(T-2)*mean(Exp2,2,'omitnan')./std(Exp2,[],2,'omitnan');
    R2.NPfactors1(1,K) = sum(abs(tt)<1.96);

    R2.TotalR2_i(1,K) = r2.TotalR2_i;
    R2.PredR2_i(1,K) = r2.PredR2_i;
    R2.TotalR2_x(1,K) = r2.TotalR2_x;
    R2.PredR2_x(1,K) = r2.PredR2_x;
    R2.TimeSeriesR2_i(1,K) = r2.TimeSeriesR2_i;
    R2.CrossSectionR2_i(1,K) = r2.CrossSectionR2_i;
    R2.TimeSeriesR2_x(1,K) = r2.TimeSeriesR2_x;
    R2.CrossSectionR2_x(1,K) = r2.CrossSectionR2_x;
    R2.RelPrcErr_i(1,K) = r2.RelPrcErr_i;
    R2.RelPrcErr_x(1,K) = r2.RelPrcErr_x;

    % alpha version
    [G, F] = IPCA(X,W,Nt,K,ones(1,T));
    r2 = IPCA_R2(G,F,Return,Z,IsF,X,W,ones(1,T));
    R2.TotalR2_i_alpha(1,K) = r2.TotalR2_i;
    R2.PredR2_i_alpha(1,K) = r2.PredR2_i;
    R2.TotalR2_x_alpha(1,K) = r2.TotalR2_x;
    R2.PredR2_x_alpha(1,K) = r2.PredR2_x;

    % bootstrap test
    [pval_alpha(1,K),~,~] = IPCA_gamma_delta_pvalue(X,W,Nt,G,F,ones(1,T),Nboot);
end

Vars{1,1} = 'Total R$^2$';
Vars{2,1} = 'Time Series R$^2$';
Vars{3,1} = 'Cross Section R$^2$';
Vars{4,1} = 'Relative Pricing Error';
disp('\cmidrule{2-8}')

disp('\emph{Stock Level Option Positions:}\\')
XX = [R2.TotalR2_i;R2.TimeSeriesR2_i;R2.CrossSectionR2_i;R2.RelPrcErr_i];

for i = 1:size(XX,1)
    bb = [];
    for j = 1:size(XX,2)-1
        if ~isnan(XX(i,j))
            bb = [bb '&' num2str(100*XX(i,j),'%1.2f')];
        else
            bb = [bb '&'];
        end
    end
    disp([Vars{i,1} bb '\\'])
end
disp('\\')

disp('\emph{Managed Portfolios:}\\')
XX = [R2.TotalR2_x;R2.TimeSeriesR2_x;R2.CrossSectionR2_x;R2.RelPrcErr_x];

for i = 1:size(XX,1)
    bb = [];
    for j = 1:size(XX,2)-1
        if ~isnan(XX(i,j))
            bb = [bb '&' num2str(100*XX(i,j),'%1.2f')];
        else
            bb = [bb '&'];
        end
    end
    disp([Vars{i,1} bb '\\'])
end
disp('\\')
disp('\emph{Bootstrap test:}\\')
XX = pval_alpha;
for i = 1:size(XX,1)
    bb = [];
    for j = 1:size(XX,2)-1
        if ~isnan(XX(i,j))
            bb = [bb '&' num2str(100*XX(i,j),'%1.2f')];
        else
            bb = [bb '&'];
        end
    end
    disp(['$W_\alpha$ \ $p$-value' bb '\\'])
end

XX = R2.NPfactors;
for i = 1:size(XX,1)
    bb = [];
    for j = 1:size(XX,2)-1
        if ~isnan(XX(i,j))
            bb = [bb '&' num2str(XX(i,j),'%1.0f')];
        else
            bb = [bb '&'];
        end
    end
    disp(['\# Unpriced Factors' bb '\\'])
end
disp('\midrule')


%% OUT of sample
for k = 1:6
    r2 = IPCA_oos(Return,Z,IsF,X,W,Nt,k);
    TotalR2_i(1,k) = r2.TotalR2_i;
    PredR2_i(1,k) = r2.PredR2_i;
    TotalR2_x(1,k) = r2.TotalR2_x;
    PredR2_x(1,k) = r2.PredR2_x;
    TimeSeriesR2_i(1,k) = r2.TimeSeriesR2_i;
    CrossSectionR2_i(1,k) = r2.CrossSectionR2_i;
    TimeSeriesR2_x(1,k) = r2.TimeSeriesR2_x;
    CrossSectionR2_x(1,k) = r2.CrossSectionR2_x;
    RelPrcErr_i(1,k) = r2.RelPrcErr_i;
    RelPrcErr_x(1,k) = r2.RelPrcErr_x;
end

R2.TotalR2_i = TotalR2_i;
R2.PredR2_i = PredR2_i;
R2.TotalR2_x = TotalR2_x;
R2.PredR2_x = PredR2_x;
R2.TimeSeriesR2_i = TimeSeriesR2_i;
R2.CrossSectionR2_i = CrossSectionR2_i;
R2.TimeSeriesR2_x = TimeSeriesR2_x;
R2.CrossSectionR2_x = CrossSectionR2_x;
R2.RelPrcErr_i = RelPrcErr_i;
R2.RelPrcErr_x = RelPrcErr_x;

disp('\cmidrule{2-8}')
disp('\emph{Stock Level Option Positions:}\\')
XX = [R2.TotalR2_i;R2.TimeSeriesR2_i;R2.CrossSectionR2_i;R2.RelPrcErr_i];

for i = 1:size(XX,1)
    bb = [];
    for j = 1:size(XX,2)-1
        if ~isnan(XX(i,j))
            bb = [bb '&' num2str_math(100*XX(i,j),'%1.2f')];
        else
            bb = [bb '&'];
        end
    end
    disp([Vars{i,1} bb '\\'])
end

disp('\emph{Managed Portfolios:}\\')
XX = [R2.TotalR2_x;R2.TimeSeriesR2_x;R2.CrossSectionR2_x;R2.RelPrcErr_x];

for i = 1:size(XX,1)
    bb = [];
    for j = 1:size(XX,2)-1
        if ~isnan(XX(i,j))
            bb = [bb '&' num2str_math(100*XX(i,j),'%1.2f')];
        else
            bb = [bb '&'];
        end
    end
    disp([Vars{i,1} bb '\\'])
end
disp('\midrule')

return